package com.szp.spCloud.common.service.impl;

import com.szp.spCloud.codeService.dao.CodeRepository;
import com.szp.spCloud.common.pojo.AuthCode;
import com.szp.spCloud.common.service.CodeValidateService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;

import java.util.Date;
import java.util.List;

@Service
public class CodeValidateServiceImpl implements CodeValidateService {
    @Autowired
    CodeRepository codeRepository;
    @Override
    public Integer validateCode(String email, String code) {
        AuthCode authCode = new AuthCode();
        authCode.setEmail(email);
        authCode.setCode(code);
        Example<AuthCode> example = Example.of(authCode);
        List<AuthCode> res = codeRepository.findAll(example);
        if(null == res || res.size() == 0){
            return 1;
        }
        res.sort((item1,item2)->item1.getId() - item2.getId());
//        Collections.reverse(res);
        Date expireTime = res.get(res.size()-1).getExpireTime();
        long expireMillis = expireTime.getTime();
        if(System.currentTimeMillis() - expireMillis > 0){
            return 2;
        }else{
            return 0;
        }
    }
}
